/**
 * Copyright (c) 2008,中企动力华南研发中心
 * All rights reserved.
 */

package com.ce.pms.customer.web;

import java.text.SimpleDateFormat;

import com.ce.pms.customer.service.ICallInPersonService;
import com.ce.pms.customer.vo.CallInPersonVO;
import com.ce.pms.customer.vo.CustSearchVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * 逻辑、功能相关描述:来访人员管理ACTION
 *
 * @author 许海堆 编写日期：2008-5-26
 */
public class CallInPersonAction extends CustomerAction {
	private ICallInPersonService callInPersonService;

	/**
	 * Function:来访人员主页面跳转
	 * Create author:罗军林
	 * Create on:2008-5-28
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCallInPersonMain(@Read(key="isShowMenu", defaultValue="true")
									 @Out(key="isShowMenu") String isShowMenu,
									 @Read(key="houseNumber") @Out(key="houseNumber")String houseNumber) {
	}

	/**
	 * Function:新增页面的初始化
	 * Create author:罗军林
	 * Create on:2008-5-29
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCallInPersonAdd() {
		String currentDate = getCurrentDate();
		setAttribute("certificateList",getCertificateType());
		setAttribute("currentDate",  currentDate);
	}


	/**
	 * Function:初始化列表页面，根据业务需要，<br/>
	 * 			这里仅做页面跳转,初始时不做查询
	 * Create author:罗军林
	 * Create on:2008-6-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCallInPersonList(){
		setAttribute("totalRows", new Integer(0));
	}

	/**
	 * Function:修改页面的初始化
	 * Create author:罗军林
	 * Create on:2008-5-29
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCallInPersonEdit( @Read(key = "guestRecordId") Long guestRecordId) {
		searchCallInpersonByGuestRecordID(guestRecordId);
		setAttribute("certificateList",getCertificateType());
		setAttribute("currentDate",  getCurrentDate());
	}

	/**
	 * Function:初始化详细信息页面
	 * Create author:罗军林
	 * Create on:2008-6-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCallInPersonDetail( @Read(key = "guestRecordId") Long guestRecordId){
		searchCallInpersonByGuestRecordID(guestRecordId);
	}

	/**
	 * Function:查询来访人员列表
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-5-26
	 * Edit author:罗军林
	 * Edit on:
	 * Why:补充功能性代码
	 */
	public String callInPersonList(	@Read CustSearchVO custSearchVO,
									@Read(key = "ec_crd",defaultValue="15") int pageSize,
									@Read(key = "ec_p",defaultValue="1")	int pageNo,
									@Read(key="isShowMenu",defaultValue="true") @Out(key="isShowMenu")String isShowMenu) {

		return myCallInPersonList(custSearchVO, pageSize, pageNo);
	}

	/**
	 * Function:查询来访人员列表
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-5-26
	 * Edit author:罗军林
	 * Edit on:
	 * Why:补充功能性代码
	 */

	public String pageCallInPersonList(	@Read(key = "ec_crd",defaultValue="15") int pageSize,
										@Read(key = "ec_p",defaultValue="1")	int pageNo) {
		CustSearchVO custSearchVO = getCustSearchVOFromCache();
		return myCallInPersonList(custSearchVO, pageSize, pageNo);
	}

	/**
	 * Function:登记来访人员
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-5-26
	 * Edit author:罗军林
	 * Edit on:
	 * Why:增加功能性代码
	 */
	public String addCallInPerson(@Read	CallInPersonVO callInPersonVO) {

		callInPersonVO.setCreator(getUserId());
		callInPersonService.addCallInPerson(callInPersonVO);

		//如果是保存并新增，转到新增的页面
		if (CallInPersonVO.FLAG_ADD_AGAIN.equalsIgnoreCase(callInPersonVO
				.getSaveType())) {
			return "callInPsersonAdd";
		}

		return null;
	}

	/**
	 * Function:编辑来访人员资料
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-5-26
	 * Edit author:罗军林
	 * Edit on:
	 * Why:
	 */
	public String editCallInPerson(	@Read CallInPersonVO callInPersonVO) {
		callInPersonVO.setCreator(getUserId());
		callInPersonService.editCallInPerson(callInPersonVO);

		return null;
	}

	/**
	 * Function:删除来访人员资料
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-5-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String deleteCallInPerson( @Read(key="callInPersonId") Long callInPersonId) {
		callInPersonService.deleteCallInPerson(callInPersonId);
		//renderText("false");
		return null;
	}

	/********私有方法*******************************************/

	/**
	 * Function:根据来访人员ID调用service查询其记录，<br/>
	 * 			并将记录以"callInperson"名称写入request缓存中。
	 * @param guestRecordId
	 * Create author:罗军林
	 * Create on:2008-6-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private void searchCallInpersonByGuestRecordID(Long guestRecordId) {
		IndividualVO individualVO=callInPersonService.getCallInPersonById(guestRecordId);
		setAttribute("callInperson", individualVO);
	}

	/** Function:获取当前日期，并以yyyy-MM-dd HH:mm格式返回
	 * @return
	 * Create author:罗军林
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private String getCurrentDate() {
		String currentDate;
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		currentDate=sf.format(callInPersonService.getDate());
		return currentDate;
	}

	/** Function:执行查询动作，并将查询条件、查询结果存入缓存中
	 * @param custSearchVO
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:罗军林
	 * Create on:2008-6-12
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private String myCallInPersonList(CustSearchVO custSearchVO, int pageSize, int pageNo) {
		String sortStr = ExtremeUtils.getOrderString(this.getRequest());

		custSearchVO.setOrderString(sortStr);

		Page page = callInPersonService.queryCallInPerson(custSearchVO,pageSize, pageNo);

		setAttribute("totalRows", new Integer((int) page.getTotalCount()));
		setAttribute("custSearchVO", custSearchVO,Scope.SESSION);
		setAttribute("callInPersonList", page.getResult());
		return "callInPersonList";
	}

	/**
	 * Function:<p>从缓存中获取查询条件，如果条件为空则调用空构造</p>
	 * 			<p>函数构造一个查询条件对象并返回</p>
	 * @return
	 * Create author:罗军林
	 * Create on:2008-6-13
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private CustSearchVO getCustSearchVOFromCache() {
		Object obj=getAttribute("custSearchVO");
		CustSearchVO custSearchVO=( obj==null ? new CustSearchVO() : (CustSearchVO)obj );
		return custSearchVO;
	}

	/****service注入方法***************************************/
	public void setCallInPersonService(ICallInPersonService callInPersonService) {
		this.callInPersonService = callInPersonService;
	}

}
